package com.gvfish.service;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.gvfish.entity.Authority;
import com.gvfish.entity.User;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

//	@Autowired
//	private SecurityManager securityManager;

	@Override
	public UserDetails loadUserByUsername(String userName){
//			throws UsernameNotFoundException, DataAccessException {
//
//		User user = securityManager.findUserByLoginName(userName);
//		if (user == null)
//			throw new UsernameNotFoundException("用户" + userName + " 不存在");
//
//		System.out.println(user.getName());
//		GrantedAuthority[] grantedAuths = obtainGrantedAuthorities(user);
//
//		// zhen中无以下属性,暂时全部设为true.
//		boolean enabled = true;
//		boolean accountNonExpired = true;
//		boolean credentialsNonExpired = true;
//		boolean accountNonLocked = true;
//
//		 org.springframework.security.core.userdetails.User userdetail = new org.springframework.security.core.userdetails.User(user.getName(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths);
//		return userdetail;
		return null;
	}

	/**
	 */
	private GrantedAuthority[] obtainGrantedAuthorities(User user) {
		return null;
//		 Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
//		 for (Authority authority : securityManager.getRoles(user)) {
//		 authSet.add(new GrantedAuthorityImpl(authority.getName()));
//		 }
//		 return authSet.toArray(new GrantedAuthority[authSet.size()]);
//		 Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
//		 authSet.add(new GrantedAuthorityImpl("admin"));
//		 authSet.add(new GrantedAuthorityImpl("docAdmin"));
//		 
//		 return authSet.toArray(new GrantedAuthority[authSet.size()]);
	}
}
